package com.koron.order.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.order.common.bean.po.OrderEquipPo;
import com.koron.order.common.mapper.OrderEquipMapper;
import com.koron.order.common.service.OrderEquipService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单设备表(OrderEquip)表服务实现类
 *
 * @author zhouj
 * @since 2022-03-16 15:49:19
 */
@Service("orderEquipService")
public class OrderEquipServiceImpl implements OrderEquipService {
    @Autowired
    HttpServletRequest httpServletRequest;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, OrderEquipPo orderEquip) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.insertOrUpdate(orderEquip);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OrderEquipPo> orderEquipList) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.insertOrUpdateBatch(orderEquipList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderEquipPo orderEquip) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.insert(orderEquip);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderEquipPo> orderEquipList) {

        if (CollectionUtils.isEmpty(orderEquipList)) {
            return 0;
        }

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        List<OrderEquipPo> orderEquipPos = mapper.queryByOrderId(orderEquipList.get(0).getOrderId(),httpServletRequest.getHeader("Language"));
        if(CollectionUtils.isNotEmpty(orderEquipPos)){
            mapper.deleteByIds(orderEquipPos.stream().map(OrderEquipPo::getId).collect(Collectors.joining(",")));
        }
        return mapper.insertBatch(orderEquipList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderEquipPo orderEquip) {
        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);
        return mapper.update(orderEquip);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderEquipPo queryById(SessionFactory factory, String id) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<OrderEquipPo> queryByOrderId(SessionFactory factory, String orderId) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.queryByOrderId(orderId,httpServletRequest.getHeader("Language"));
    }

    @Override
    public List<OrderEquipPo> queryByOrderIds(SessionFactory factory, List<String> orderIds) {

        if (CollectionUtils.isEmpty(orderIds)) {
            return new ArrayList<>();
        }

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        return mapper.queryByOrderIds(orderIds,httpServletRequest.getHeader("Language"));
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderEquipPo> queryList(SessionFactory factory, OrderEquipPo orderEquip) {

        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        List<OrderEquipPo> list = mapper.queryList(orderEquip);

        return list;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderEquipPo orderEquip, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OrderEquipMapper mapper = factory.getMapper(OrderEquipMapper.class);

        List<OrderEquipPo> list = mapper.queryList(orderEquip);

        return new PageInfo(list);
    }

}